package cn.anycase.demo.sample.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import cn.anycase.demo.sample.entity.ThisIsSample;
import cn.anycase.demo.enums.GenderEnum;
import cn.anycase.demo.sample.mapper.ThisIsSampleMapper;
import cn.anycase.demo.sample.service.ThisIsSampleService;

/**
 * <p>
 * </p>
 *
 * @author yuxiaobin
 * @date 2022/8/31
 */
@Service
public class ThisIsSampleServiceImpl implements ThisIsSampleService {

    @Autowired
    ThisIsSampleMapper thisIsSampleMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void sampleCrud() {
        EntityWrapper<ThisIsSample> ew = new EntityWrapper<>();
        List<ThisIsSample> list = thisIsSampleMapper.selectList(ew);
        System.out.println("list.size=" + list.size());
        ThisIsSample sample = new ThisIsSample();
        sample.setGender(GenderEnum.MALE.getCode());
        sample.setAge(18);
        sample.setName("Hello World!");
        sample.setCreateTime(new Date());
        sample.setCreateBy("test");
        thisIsSampleMapper.insert(sample);
        list = thisIsSampleMapper.selectList(ew);
        System.out.println("list.size=" + list.size());
        System.out.println("id=" + sample.getId());
        ThisIsSample update = new ThisIsSample();
        update.setId(sample.getId());
        update.setGender(GenderEnum.FEMALE.getCode());
        thisIsSampleMapper.updateById(update);
        System.out.println("gender=" + thisIsSampleMapper.selectById(sample.getId()).getGender());
    }

    @Transactional
    @Override
    public boolean createRecord(ThisIsSample sample) {
        return thisIsSampleMapper.insert(sample) != 0;
    }
}
